{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Download Sentinel-2 Data from Planetary Computer\n",
    "\n",
    "[![image](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/opengeos/geoai/blob/main/docs/examples/download_sentinel2.ipynb)\n",
    "\n",
    "The notebook demonstrates how to download Sentinel-2 data from the [Planetary Computer](https://planetarycomputer.microsoft.com/explore) using the `geoai` package.\n",
    "\n",
    "## Install package\n",
    "To use the `geoai-py` package, ensure it is installed in your environment. Uncomment the command below if needed."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# %pip install geoai-py"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Import libraries"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import geoai"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Search data\n",
    "\n",
    "Go to the [Planetary Computer](https://planetarycomputer.microsoft.com/explore) and search for the data you are interested in. Copy the STAC item link and paste it to the code below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Example for Sentinel-2 item\n",
    "item_url = \"https://planetarycomputer.microsoft.com/api/stac/v1/collections/sentinel-2-l2a/items/S2B_MSIL2A_20250228T173149_R055_T14SLH_20250228T212633\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Download sample data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Specify which bands to download (Sentinel-2 bands)\n",
    "bands_to_download = [\n",
    "    \"B01\",\n",
    "    \"B02\",\n",
    "    \"B03\",\n",
    "    \"B04\",\n",
    "    \"B05\",\n",
    "    \"B06\",\n",
    "    \"B07\",\n",
    "    \"B08\",\n",
    "    \"B8A\",\n",
    "    \"B09\",\n",
    "    \"B11\",\n",
    "    \"B12\",\n",
    "    \"AOT\",\n",
    "    \"WVP\",\n",
    "    \"SCL\",\n",
    "]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create a directory for the downloaded bands\n",
    "output_directory = \"sentinel2_bands\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Download the bands, save them to the output directory, and create a merged GeoTIFF.\n",
    "# The download process may take a while. Please be patient.\n",
    "downloaded_bands = geoai.download_pc_stac_item(\n",
    "    item_url=item_url,\n",
    "    bands=bands_to_download,\n",
    "    output_dir=output_directory,\n",
    "    show_progress=True,\n",
    "    merge_bands=True,\n",
    "    merged_filename=\"sentinel2_all_bands.tif\",\n",
    "    overwrite=False,  # Skip files that already exist\n",
    "    cell_size=10,  # Resample all bands to 10m resolution\n",
    ")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Print the paths to the downloaded files\n",
    "for band, path in downloaded_bands.items():\n",
    "    print(f\"Downloaded {band}: {path}\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Visualize data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "raster_url = \"https://huggingface.co/datasets/giswqs/geospatial/resolve/main/sentinel2a_kansas.tif\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "geoai.view_raster(raster_url, bidx=[8, 4, 3], rescale=\"1000,4000\")"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "geo",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.9"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
